/* ajax请求 */
function $ajax(options) {
    options = Object.assign({
        url: '',
        method: 'post',
        headers: {},
        data: null,
        progress: Function.prototype
    }, options);
    return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest;
        xhr.upload.onprogress = options.progress;
        xhr.open(options.method, options.url);
        Object.keys(options.headers).forEach(key => {
            xhr.setRequestHeader(key, options.headers[key]);
        })
        xhr.send(options.data);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (/^(2|3)\d{2}/.test(xhr.status)) {
                    resolve(JSON.parse(xhr.responseText));
                } else {
                    reject(xhr);
                }
            }
        }
    })
}
/* 文件名 */
function $formatFileName(fileName) {
    let dotIndex = fileName.lastIndexOf('.'),
        name = fileName.slice(0, dotIndex),
        suffix = fileName.slice(dotIndex + 1);
    name = md5(name) + new Date().getTime();
    return {
        hash: name,
        suffix,
        filename: `${name}.${suffix}`
    }
}
